<?php
import( 'de.ceus-media.framework.neon.View' );
import( 'de.ceus-media.database.StatementBuilder' );
import( 'classes.models.Bug' );
import( 'classes.models.BugStatements' );
/**
 *	Builds different Graphs with Configuration.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_View
 *	@uses			Database_StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			13.06.2007
 *	@version		0.1
 */
/**
 *	Builds different Graphs with Configuration.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_View
 *	@uses			Database_StatementBuilder
 *	@uses			BugStatements
 *	@uses			Bug
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			13.06.2007
 *	@version		0.1
 */
class GraphViews extends CMF_Krypton_View
{
	/**
	 *	Builds Graph Content.
	 *	@access		public
	 *	@param		string		$type		Type of Graph (must be definied in graphs.ini)
	 *	@return 	string
	 */
	public function buildContent( $type )
	{
		require_once( JPGRAPH_PATH.'jpgraph.php' );
		require_once( JPGRAPH_PATH.'jpgraph_pie.php');
		require_once( JPGRAPH_PATH.'jpgraph_pie3d.php' );

		$config		=& $this->ref->get( 'config' );	
		$session	= $this->ref->get( 'session' );	
		$request	= $this->ref->get( 'request' );

		$this->loadLanguage( 'main' );

		$uri	= $config['paths']['themes'].$session->get( 'theme' )."/graphs.ini";
		if( file_exists( $uri ) )
		{
			$graphs	= parse_ini_file( $uri, true );
			foreach( $graphs as $graph => $settings )
				foreach( $settings as $key => $value )
					$config['graphs'][$graph][$key]	= $value;
		}
		$config['graphs'][$type]['colors']	= explode( ",", $config['graphs'][$type]['colors'] );

		$timestamp	= 0;
		$bug		= new Bug();
		$data		= array_pop( $bug->getAllData( array( 'modified' ), array(), array( 'modified' => "DESC" ) ) );
		if( $data )
			$timestamp	= $data[0]['modified'];
			
		$cacheFile	= $config['paths']['cache']."graph_".$type;
		if( !( file_exists( $cacheFile ) &&  $timestamp <= filemtime( $cacheFile ) ) )
		{
			$content	= $this->buildGraph( $type );
			file_put_contents( $cacheFile, $content );
			return $content;
		}
		else
			return file_get_contents( $cacheFile );	
	}

	/**
	 *	Builds HTML of Graph (with Link Map).
	 *	@access		private
	 *	@param		string		$type		Type of Graph (must be definied in graphs.ini)
	 *	@return 	string
	 */
	private function buildGraph( $type )
	{
		$config			= $this->ref->get( 'config' );
		$request		= $this->ref->get( 'request' );
		$graphConfig	= $config['graphs'][$type];
		$words			= $this->words['main']['home'];

		$statistics		= $this->getStatistics( $type );

		$data	= array();
		$legend	= array();
		$targ	= array();
		$alts	= array();
		$data	= array();
		$total	= 0;
		foreach( $statistics as $stat )
		{
			$data[]		= $stat['count'];
			$legend[]	= $stat['name']." (".$stat['count'].")";
			$targ[]		= "?link=bug&resetFilters&filterBugs&".$graphConfig['filter_key']."=".$stat[$type];
			$alts[]		= $stat['name'];
			$total		+= $stat['count'];
		}
		if( !$total )
			return "No bugs found";
		$graph = new PieGraph( $graphConfig['width'], $graphConfig['height'], $graphConfig['graph_name'] );
//		$graph->SetShadow( $graphConfig['shadow'] );
		$graph->SetAntiAliasing( $graphConfig['antialias'] );
		$graph->title->Set( $words[$graphConfig['title_key']] );
//		$graph->title->SetFont( FF_VERDANA,FS_NORMAL, 11 );
		$graph->title->SetFont( FF_FONT1, FS_BOLD );
		$graph->title->Pos();
		$graph->legend->Pos( $graphConfig['legend_margin_x'], $graphConfig['legend_margin_y'], $graphConfig['legend_align_x'], $graphConfig['legend_align_y'] );
//		$graph->legend->SetShadow( $graphConfig['legend_shadow'] );
		$graph->legend->SetShadow( false );
//		$graph->legend->SetFont( FF_VERDANA, FS_NORMAL, 8 );
		$graph->legend->SetFont( FF_FONT1, FS_NORMAL, 8 );
		$p1 = new PiePlot3D( $data );
		$p1->value->SetFormat( "%d%%" );
		$p1->value->Show();
//		$p1->value->SetFont( FF_VERDANA, FS_NORMAL, 8 );
		$p1->value->SetFont( FF_FONT1, FS_NORMAL, 7 );
		$p1->SetLegends( $legend );
		$p1->SetCSIMTargets( $targ, $alts );
		$p1->setSliceColors( $graphConfig['colors'] );
		$p1->SetCenter( $graphConfig['center_x'], $graphConfig['center_y'] );
		$graph->Add( $p1 );
		$url	= $config['paths']['images'].$graphConfig['file_name'];
		$graph->Stroke( $url );
		$name	= $graphConfig['image_name'];
		$map	= $graph->GetHTMLImageMap( $name );
		$image	= $map."<img align='right' src='".$url."?".time()."' ISMAP USEMAP='#".$name."' border='0'>";
		return $map.$image;
		$ui['image']	= $map.$image;
		return $this->loadTemplate( $graphConfig['template'], $ui );
	}

	/**
	 *	Returns statistical Information.
	 *	@access		private
	 *	@param		string		$type		Type of Graph (must be defined in graphs.ini)
	 *	@return		string
	 */
	private function getStatistics( $type )
	{
		$dbc	= $this->ref->get( 'dbc' );
		$config	= $this->ref->get( 'config' );
		$sb	= new Database_StatementBuilder( $config['config']['table_prefix'] );
		$bs	= new BugStatements( $sb );
		$bs->addComponent( "Base" );
		$query	= $sb->buildStatement();
		$result	= $dbc->execute( $query );

		$method	= "get".ucFirst( $type )."Statistics";
		return $this->$method( $result );
	}
	
	/**
	 *	Returns Statistics for 'Work Graph'.
	 *	@access		private
	 *	@param		MySqlRequest	$result		Result Set from Database
	 *	@return 	array
	 */
	private function getWorkStatistics( $result )
	{
		$stats	= array(
			-1	=> array(
				"work"	=> -1,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][-1]
				),
			0	=> array(
				"work"	=> 0,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][0]
				),
			1	=> array(
				"work"	=> 1,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][1]
				),
			2	=> array(
				"work"	=> 2,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][2]
				),
			3	=> array(
				"work"	=> 3,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][3]
				),
			4	=> array(
				"work"	=> 4,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][4]
				),
			);
		while( $bug	= $result->fetchNextObject( false ) )
			$stats[$bug->status]['count'] ++;
		foreach( $stats as $status => $data )
			$stats[$status]['status']	= $status;
//		ksort( $stats );
		unset( $stats[4] );
		$tmp	= $stats[2];
		$stats[2]	= $stats[3];
		$stats[3]	= $tmp;
		return $stats;
	}

	/**
	 *	Returns Statistics for 'Status Graph'.
	 *	@access		private
	 *	@param		MySqlRequest	$result		Result Set from Database
	 *	@return 	array
	 */
	private function getStatusStatistics( $result )
	{
		$stats	= array(
			-1	=> array(
				"status"	=> -1,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][-1]
				),
			0	=> array(
				"status"	=> 0,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][0]
				),
			1	=> array(
				"status"	=> 1,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][1]
				),
			2	=> array(
				"status"	=> 2,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][2]
				),
			3	=> array(
				"status"	=> 3,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][3]
				),
			4	=> array(
				"status"	=> 4,
				"count"	=> 0,
				"name"	=> $this->words['main']['states'][4]
				),
			);
		while( $bug	= $result->fetchNextObject( false ) )
			$stats[$bug->status]['count'] ++;
		foreach( $stats as $status => $data )
			$stats[$status]['status']	= $status;
		ksort( $stats );
		return $stats;
	}

	/**
	 *	Returns Statistics for 'Priority Graph'.
	 *	@access		private
	 *	@param		MySqlRequest	$result		Result Set from Database
	 *	@return 	array
	 */
	private function getPriorityStatistics( $result )
	{
		$prios	= array(
			1	=> array(
				"priority"	=> 1,
				"count"	=> 0,
				"name"	=> $this->words['main']['priorities'][1]
				),
			2	=> array(
				"priority"	=> 2,
				"count"	=> 0,
				"name"	=> $this->words['main']['priorities'][2]
				),
			3	=> array(
				"priority"	=> 3,
				"count"	=> 0,
				"name"	=> $this->words['main']['priorities'][3]
				),
			4	=> array(
				"priority"	=> 4,
				"count"	=> 0,
				"name"	=> $this->words['main']['priorities'][4]
				),
			5	=> array(
				"priority"	=> 5,
				"count"	=> 0,
				"name"	=> $this->words['main']['priorities'][5]
				),
			);
		while( $bug	= $result->fetchNextObject( false ) )
			$prios[$bug->priority]['count'] ++;
		ksort( $prios );
		return $prios;
	}
	
	/**
	 *	Returns Statistics for 'Type Graph'.
	 *	@access		private
	 *	@param		MySqlRequest	$result		Result Set from Database
	 *	@return 	array
	 */
	private function getTypeStatistics( $result )
	{
		$types	= array(
			1	=> array(
				"type"	=> 0,
				"count"	=> 0,
				"name"	=> $this->words['main']['types'][0]
				),
			2	=> array(
				"type"	=> 1,
				"count"	=> 0,
				"name"	=> $this->words['main']['types'][1]
				),
			3	=> array(
				"type"	=> 2,
				"count"	=> 0,
				"name"	=> $this->words['main']['types'][2]
				),
			4	=> array(
				"type"	=> 3,
				"count"	=> 0,
				"name"	=> $this->words['main']['types'][3]
				),
			5	=> array(
				"type"	=> 4,
				"count"	=> 0,
				"name"	=> $this->words['main']['types'][4]
				),
			);
		while( $bug	= $result->fetchNextObject( false ) )
			$types[$bug->type+1]['count'] ++;
		ksort( $types );
		return $types;
	}
}
?>